package com.cskaoyan.service.goods;

import com.cskaoyan.bean.bo.goodsbo.*;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.bean.po.*;
import com.cskaoyan.bean.vo.goodsvo.BrandVO;
import com.cskaoyan.bean.vo.goodsvo.CatAndBrandVO;
import com.cskaoyan.bean.vo.goodsvo.Category;
import com.cskaoyan.bean.vo.goodsvo.QueryGoodsDetailsVo;
import com.cskaoyan.mapper.*;
import com.cskaoyan.util.StringUtil2;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description:todo
 * @Author:todo wang xv
 * @Date:todo 2022/11/20
 */
@Service
public class GoodServiceImpl implements GoodsService {


    // 商品mapper
    @Autowired
    MarketGoodsMapper marketGoodsMapper;
    // 类目mapper
    @Autowired
    MarketCategoryMapper marketCategoryMapper;
    // 品牌mapper；
    @Autowired
    MarketBrandMapper marketBrandMapper;
    // 商品参数mapper
    @Autowired
    MarketGoodsSpecificationMapper specificationMapper;
    // 商品货品mapper;
    @Autowired
    MarketGoodsProductMapper productMapper;
    // 商品参数表；
    @Autowired
    MarketGoodsAttributeMapper attributeMapper;


    @Override
    public CommonData<MarketGoods> queryGoodsList(BasePageInfoQueryListBo info) {

        // 开启分页；
        if (info.getPage() != null && info.getPage() != 0 && info.getLimit() != null && info.getLimit() != 0) {
            PageHelper.startPage(info.getPage(), info.getLimit());
        } else if (info.getPage() != null && info.getPage() != 0) {
            PageHelper.startPage(info.getPage());
        }
        // 做goods表做排序分页查询
        MarketGoodsExample example = new MarketGoodsExample();

        MarketGoodsExample.Criteria criteria = example.createCriteria();


        if (!StringUtil2.isEmpty(info.getGoodsSn())) {
            criteria.andGoodsSnEqualTo(info.getGoodsSn().trim());
        }

        if (!StringUtil2.isEmpty(info.getGoodsId())) {
            criteria.andIdEqualTo(Integer.parseInt(info.getGoodsId().trim()));
        }
        if (!StringUtil2.isEmpty(info.getName())) {
            criteria.andNameLike("%" + info.getName().trim() + "%");
        }
        // 排序
        if (info.getOrder() != null && info.getSort() != null) {
            example.setOrderByClause(info.getSort() + " " + info.getOrder());
        }

        List<MarketGoods> marketGoods = marketGoodsMapper.selectByExample(example);
        // 将查询结果作为有参构造方法参数传入，获得pageInfo
        PageInfo<MarketGoods> pageInfo = new PageInfo<>(marketGoods);
        int page = pageInfo.getPageNum();
        long total = pageInfo.getTotal();
        int pages = pageInfo.getPages();
        int limit = pageInfo.getPageSize();
        List<MarketGoods> list = pageInfo.getList();
        //获得所需要返回的参数并赋值；
        CommonData<MarketGoods> commonData = new CommonData<>();
        commonData.setTotal((int) total);
        commonData.setPages(pages);
        commonData.setLimit(limit);
        commonData.setPage(page);
        commonData.setList(list);
        return commonData;
    }

    /**
     * @Description:todo 添加商品时显示商品的规格；
     * @Author:todo wang xv
     * @Date:todo 2022/11/20
     */
    @Override
    public CatAndBrandVO queryCatAndBrandList() {


        // 查询所有品牌的信息；
        MarketBrandExample brandExample = new MarketBrandExample();
        List<MarketBrand> marketBrands = marketBrandMapper.selectByExample(brandExample);
        // 将信息赋值
        CatAndBrandVO catAndBrandVO = new CatAndBrandVO();
        List<BrandVO> brandList = new ArrayList<>();
        for (MarketBrand marketBrand : marketBrands) {

            brandList.add(new BrandVO(marketBrand.getId(), marketBrand.getName()));
        }

        // 查询所有的父亲类目的信息；
        Integer pid = 0;
        List<Category> categoryList = marketCategoryMapper.selectIdAndName(pid);
        for (Category category : categoryList) {
            // 查询出所有子类目类的信息；
            category.setChildren(marketCategoryMapper.selectIdAndName(category.getValue()));
        }
        catAndBrandVO.setCategoryList(categoryList);
        catAndBrandVO.setBrandList(brandList);
        return catAndBrandVO;

    }

    /**
     * @Description:todo 添加商品的逻辑
     * @Author:todo wang xv
     * @Date:todo 2022/11/20
     */
    @Override
    public void insertGoods(GoodsCreateBO goodsCreateBO) {
        MarketGoods goods = goodsCreateBO.getGoods();
        // 遍历product获得最低价；
        List<MarketGoodsProduct> products = goodsCreateBO.getProducts();
        BigDecimal minMoney = products.get(0).getPrice();
        for (int i = 0; i < products.size(); i++) {
            if (products.get(i).getPrice().compareTo(minMoney) < 0) {
                minMoney = products.get(i).getPrice();
            }
        }
        // 获得真实money
        if (minMoney != null && !minMoney.equals("")) {
            goods.setRetailPrice(minMoney);
        }
        goods.setAddTime(new Date());
        marketGoodsMapper.insert(goods);
        Integer goodsId = goods.getId();

        List<MarketGoodsAttribute> attributes = goodsCreateBO.getAttributes();
        for (MarketGoodsAttribute attribute : attributes) {
            attribute.setGoodsId(goodsId);
            attribute.setAddTime(new Date());
            attributeMapper.insert(attribute);
        }

        for (MarketGoodsProduct product : products) {
            product.setAddTime(new Date());
            product.setGoodsId(goodsId);
            productMapper.insert(product);
        }

       /* // 插入specification
        List<SpecBO> specifications = goodsCreateBO.getSpecifications();
        for (SpecBO specification : specifications) {
            specification.setGoodsId(goodsid);
            specificationMapper.insertSpec(specification);
        }*/
        List<MarketGoodsSpecification> specifications = goodsCreateBO.getSpecifications();
        for (MarketGoodsSpecification specification : specifications) {
            specification.setAddTime(new Date());
            specification.setGoodsId(goodsId);
            specificationMapper.insert(specification);
        }


    }

    /**
     * @Description:todo 根据shangp信息删除商品
     * @Author:todo wang xv
     * @Date:todo 2022/11/20
     */
    @Override
    public void deleteGoods(MarketGoods goods) {
        Integer goodsId = goods.getId();
        // 根据商品的id删除商品信息
        marketGoodsMapper.deleteByPrimaryKey(goodsId);
        // 根据goodsid删除掉商品的参数；
        MarketGoodsSpecificationExample specificationExample = new MarketGoodsSpecificationExample();
        MarketGoodsSpecificationExample.Criteria criteria = specificationExample.createCriteria();
        criteria.andGoodsIdEqualTo(goodsId);
        specificationMapper.deleteByExample(specificationExample);

        // 根据goodsid删除商品货品；
        MarketGoodsProductExample productExample = new MarketGoodsProductExample();
        MarketGoodsProductExample.Criteria criteria1 = productExample.createCriteria();
        criteria1.andGoodsIdEqualTo(goodsId);
        productMapper.deleteByExample(productExample);

        // 根据goodsId删除商品参数表
        MarketGoodsAttributeExample attributeExample = new MarketGoodsAttributeExample();
        MarketGoodsAttributeExample.Criteria criteria2 = attributeExample.createCriteria();
        criteria2.andGoodsIdEqualTo(goodsId);
        attributeMapper.deleteByExample(attributeExample);
    }

    /**
     * @Description:todo 编辑商品时回显数据
     * @Author:todo wang xv
     * @Date:todo 2022/11/21
     */
    @Override
    public QueryGoodsDetailsVo queryGoodsDetailById(Integer id) {
        // 根据商品id查询商品的数据;
        MarketGoods goods = marketGoodsMapper.selectByPrimaryKey(id);
        // 根据商品id查询商品的spec；
        MarketGoodsSpecificationExample specificationExample = new MarketGoodsSpecificationExample();
        MarketGoodsSpecificationExample.Criteria criteria = specificationExample.createCriteria();
        criteria.andGoodsIdEqualTo(id);
        List<MarketGoodsSpecification> specifications = specificationMapper.selectByExample(specificationExample);
        // 根据商品id查询商品的attributes；
        MarketGoodsAttributeExample attributeExample = new MarketGoodsAttributeExample();
        MarketGoodsAttributeExample.Criteria criteria1 = attributeExample.createCriteria();
        criteria1.andGoodsIdEqualTo(id);
        List<MarketGoodsAttribute> attributes = attributeMapper.selectByExample(attributeExample);
        // 根据商品id查询商品的products；
        MarketGoodsProductExample productExample = new MarketGoodsProductExample();
        MarketGoodsProductExample.Criteria criteria2 = productExample.createCriteria();
        criteria2.andGoodsIdEqualTo(id);
        List<MarketGoodsProduct> products = productMapper.selectByExample(productExample);
        // 根据categoryId查询和pid；
        Integer[] categoryIds=null;
        try {
            Integer categoryId = goods.getCategoryId();
            MarketCategory category = marketCategoryMapper.selectByPrimaryKey(categoryId);
            categoryIds =new Integer[] {category.getPid(), categoryId};
        }catch (NullPointerException e){
            e.printStackTrace();
        }
        QueryGoodsDetailsVo goodsDetailsVo = new QueryGoodsDetailsVo(goods, attributes, specifications, products, categoryIds);
        return goodsDetailsVo;
    }

    /**
     * @Description:todo 修改商品的逻辑
     * @Author:todo wang xv
     * @Date:todo 2022/11/21
     */
    @Override
    public void updateGoods(GoodsCreateBO goodsCreateBO) {
        // 根据商品id来修该商品信息
        MarketGoods goods = goodsCreateBO.getGoods();
        // 添加修改时间；
        List<MarketGoodsProduct> products = goodsCreateBO.getProducts();
        BigDecimal minMoney = products.get(0).getPrice();
        for (int i = 0; i < products.size(); i++) {
            if (products.get(i).getPrice().compareTo(minMoney) < 0) {
                minMoney = products.get(i).getPrice();
            }
        }
        // 获得真实money
        if (minMoney != null && !minMoney.equals("")) {
            goods.setRetailPrice(minMoney);
        }

        // 获取到他添加的时间；
        Date addtime = marketGoodsMapper.getAddtime(goods.getId());
        goods.setAddTime(addtime);
        goods.setUpdateTime(new Date());

        marketGoodsMapper.updateByPrimaryKey(goods);
        // 根据id来修改spec;
        List<MarketGoodsSpecification> specifications = goodsCreateBO.getSpecifications();
        for (MarketGoodsSpecification specification : specifications) {
            specification.setUpdateTime(new Date());
            specificationMapper.updateByPrimaryKey(specification);
        }
        // 根据id修改products

        for (MarketGoodsProduct product : products) {
            product.setUpdateTime(new Date());
            productMapper.updateByPrimaryKey(product);
        }
        // 根据id修改attributes
        List<MarketGoodsAttribute> attributes = goodsCreateBO.getAttributes();
        for (MarketGoodsAttribute attribute : attributes) {
            attribute.setUpdateTime(new Date());
            attributeMapper.updateByPrimaryKey(attribute);
        }

    }
}
